/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.service.impl;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.lehe.common.Contants;
import com.lehe.common.exceptions.LHServiceException;
import com.lehe.entry.wx.message.RespBaseMessage;
import com.lehe.entry.wx.message.RespMenuQueryMessage;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 类的功能描述<br>
 *
 * @author 王丹
 * @FileName MenuQueryExecuteService.java<br>
 * @Language Java<br>
 * @date 2016-08-24<br>
 */
@Service("menuQueryExecuteService")
public class MenuQueryExecuteService extends WeChatGetExecuteService {

    @Override
    protected String queryRequestUrl() throws LHServiceException {
        return Contants.WE_CHAT_QUERY_MENU_GET + String.format(Contants.WE_CHAT_ACCESS_TOKEN_PARAM, tokenService.getToken());
    }

    @Override
    protected RespBaseMessage convertRespMessage(String respHtml) throws LHServiceException {
        return convertRespJson(respHtml, RespMenuQueryMessage.class);
    }

    public static void main(String[] args) {
        String s = "{\n" +
                "    \"menu\": {\n" +
                "        \"button\": [\n" +
                "            {\n" +
                "                \"type\": \"view\",\n" +
                "                \"name\": \"快诺商城\",\n" +
                "                \"url\": \"http://wap.kwiner.com\",\n" +
                "                \"sub_button\": []\n" +
                "            },\n" +
                "            {\n" +
                "                \"type\": \"view\",\n" +
                "                \"name\": \"下载app\",\n" +
                "                \"url\": \"http://wap.kwiner.com/download/app\",\n" +
                "                \"sub_button\": []\n" +
                "            },\n" +
                "            {\n" +
                "                \"type\": \"view\",\n" +
                "                \"name\": \"兑换积分\",\n" +
                "                \"url\": \"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5af353b3e0a40ede&redirect_uri=http%3A%2F%2Fwap.kwiner.com%2Fhb&response_type=code&scope=snsapi_base#wechat_redirect\",\n" +
                "                \"sub_button\": []\n" +
                "            }\n" +
                "        ],\n" +
                "        \"menuid\": 420036565\n" +
                "    },\n" +
                "    \"conditionalmenu\": [\n" +
                "        {\n" +
                "            \"button\": [\n" +
                "                {\n" +
                "                    \"type\": \"view\",\n" +
                "                    \"name\": \"摇奖\",\n" +
                "                    \"url\": \"http://test.kwiner.com/win\",\n" +
                "                    \"sub_button\": []\n" +
                "                },\n" +
                "                {\n" +
                "                    \"type\": \"view\",\n" +
                "                    \"name\": \"投票\",\n" +
                "                    \"url\": \"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5af353b3e0a40ede&redirect_uri=http://test.kwiner.com/vote&response_type=code&scope=snsapi_base#wechat_redirect\",\n" +
                "                    \"sub_button\": []\n" +
                "                },\n" +
                "                {\n" +
                "                    \"type\": \"view\",\n" +
                "                    \"name\": \"帮助\",\n" +
                "                    \"url\": \"http://test.kwiner.com/help\",\n" +
                "                    \"sub_button\": []\n" +
                "                }\n" +
                "            ],\n" +
                "            \"matchrule\": {\n" +
                "                \"group_id\": \"100\"\n" +
                "            },\n" +
                "            \"menuid\": 403918007\n" +
                "        },\n" +
                "        {\n" +
                "            \"button\": [\n" +
                "                {\n" +
                "                    \"type\": \"view\",\n" +
                "                    \"name\": \"摇奖\",\n" +
                "                    \"url\": \"http://test.kwiner.com/win\",\n" +
                "                    \"sub_button\": []\n" +
                "                },\n" +
                "                {\n" +
                "                    \"type\": \"view\",\n" +
                "                    \"name\": \"投票\",\n" +
                "                    \"url\": \"http://test.kwiner.com/vote\",\n" +
                "                    \"sub_button\": []\n" +
                "                },\n" +
                "                {\n" +
                "                    \"type\": \"view\",\n" +
                "                    \"name\": \"帮助\",\n" +
                "                    \"url\": \"http://test.kwiner.com/help\",\n" +
                "                    \"sub_button\": []\n" +
                "                }\n" +
                "            ],\n" +
                "            \"matchrule\": {\n" +
                "                \"group_id\": \"100\"\n" +
                "            },\n" +
                "            \"menuid\": 402709504\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
            mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            RespMenuQueryMessage resp =  mapper.readValue(s, RespMenuQueryMessage.class);
            System.out.println(resp);
            System.out.println(mapper.writeValueAsString(resp));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
